package com.android.equipment.ui.dialog;

import android.content.Context;

import androidx.annotation.NonNull;

import com.android.equipment.R;
import com.android.equipment.dao.operate.DaoUser;
import com.android.equipment.databinding.DialogUpdatePassBinding;
import com.android.equipment.utils.ToastUtil;
import com.blankj.utilcode.util.StringUtils;
import com.lxj.xpopup.core.CenterPopupView;

public class UpdatePassDialog extends CenterPopupView {
    DialogUpdatePassBinding binding;
    public UpdatePassDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_update_pass;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        binding = DialogUpdatePassBinding.bind(getPopupImplView());
        binding.tvCancel.setOnClickListener(v ->{
            dismiss();
        });

        binding.tvConfirm.setOnClickListener(v ->{
            String pass = binding.etPass.getText().toString();
            String againPass = binding.etPassAgain.getText().toString();
            String oldPass = binding.etOldPass.getText().toString();
            String userPassWord = DaoUser.getInstance().getUserPassWord();
            if (StringUtils.isEmpty(oldPass)){
                ToastUtil.showToastSafe("请输入旧密码");
                return;
            }

            if (!oldPass.equals(userPassWord)){
                ToastUtil.showToastSafe("旧密码错误");
                return;
            }

            if (StringUtils.isEmpty(pass)){
                ToastUtil.showToastSafe("请输入新密码");
                return;
            }

            if (StringUtils.isEmpty(againPass)){
                ToastUtil.showToastSafe("请再次输入新密码");
                return;
            }

            if (!pass.equals(againPass)){
                ToastUtil.showToastSafe("两次密码不一致");
                return;
            }
            if (DaoUser.getInstance().updateUserPass(pass)){
                ToastUtil.showToastSafe("修改成功");
                dismiss();
            }else {
                ToastUtil.showToastSafe("修改失败,数据库User表错误");
            }
        });
    }
}
